<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.DriverManager" %><%--
  Created by IntelliJ IDEA.
  User: lmj
  Date: 2019/3/27
  Time: 7:18
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<%
    request.setCharacterEncoding("utf8");

    Cookie[] loginCookie =request.getCookies();
    if (loginCookie!=null){
        for (Cookie cookie:loginCookie){
            if (cookie.getName().equals("username")){
               cookie.setValue("");
               response.addCookie(cookie);
            }
            if (cookie.getName().equals("password")){
                cookie.setValue("");
                response.addCookie(cookie);
            }
        }
    }
%>
<body>

<%
HttpSession session1=request.getSession();
String username= (String) session1.getAttribute("username");


    final String DBDRIVER = "com.mysql.cj.jdbc.Driver";
    final String DBURL = "jdbc:mysql://127.0.0.1:3306/javaweb?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false";
    final String DBUSER = "root";
    final String DBPASS = "root";

    Connection conn;
    PreparedStatement ps;
    ResultSet rs;
    try {
        Class.forName(DBDRIVER);
        conn = DriverManager.getConnection(DBURL, DBUSER, DBPASS);

        /**
         *从数据库删除用户
         */
        ps = conn.prepareStatement(
                "delete from user where username=?");
        ps.setString(1, username);
        ps.executeUpdate();
    }catch (Exception e){out.println(e.toString());}
        /**
         * 清除session
         */
        session1.invalidate();
response.sendRedirect("/loginAndRegister.jsp");
%>
</body>
</html>
